document.addEventListener('DOMContentLoaded', function() {
    const urlParams = new URLSearchParams(window.location.search);
    const blogId = urlParams.get('id');
    if (!blogId) {
        alert('博客ID未提供');
        return;
    }

    function fetchBlogDetails() {
        fetch(`http://101.34.242.206:9990/Blogs/getById/${blogId}`)
            .then(response => response.json())
            .then(data => {
                if (data.code === 200) {
                    const blog = data.data;
                    document.getElementById('cover').src = blog.coverurl;
                    document.getElementById('title').textContent = blog.title;
                    document.getElementById('author').innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-user"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path><circle cx="12" cy="7" r="4"></circle></svg> ${blog.author}`;
                    document.getElementById('createtime').innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-calendar"><rect x="3" y="4" width="18" height="18" rx="2" ry="2"></rect><line x1="16" y1="2" x2="16" y2="6"></line><line x1="8" y1="2" x2="8" y2="6"></line><line x1="3" y1="10" x2="21" y2="10"></line></svg> ${new Date(blog.createtime).toLocaleDateString()}`;
                    document.getElementById('views').innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-eye"><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"></path><circle cx="12" r="3"></circle></svg> ${blog.views || 0} 次浏览`;

                    document.getElementById('content').innerHTML = marked.parse(blog.content);
                    document.querySelectorAll('pre code').forEach((block) => {
                        hljs.highlightBlock(block);
                    });
                } else {
                    alert('获取博客详情失败');
                }
            })
            .catch(error => console.error('Error fetching blog details:', error));
    }

    fetchBlogDetails();
});
